package com.sky.controller.admin;

import com.sky.properties.AliOssProperties;
import com.sky.result.Result;
import com.sky.utils.AliOssUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;
@Slf4j
@RestController
@Api(tags = "通用接口")
public class CommonController {
    @Autowired
    AliOssUtil aliOssUtil;
    @ApiOperation("文件上传")
    @PostMapping("/admin/common/upload")
    public Result<String> upload(MultipartFile file) throws IOException {
        log.info(String.valueOf(aliOssUtil));
    //提交，并且返回路径
        try{
        String originalFilename= file.getOriginalFilename();
        //截取尾部后缀1
        String extend=originalFilename.substring(originalFilename.lastIndexOf("."));
        //新文件名
        String objectNme=UUID.randomUUID()+extend;
    String url=aliOssUtil.upload(file.getBytes(),objectNme);
    return Result.success(url);}
        catch (IOException e)
        {log.info("文件上传失败"+e);
        }
        return Result.error("文件上传失败");
    }
}
